﻿using MDIDemo.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace DevMy.Vou
{
    public partial class Form_UseBase : Form_BaseDialog
    {
        public Form_UseBase()
        {
            InitializeComponent();
        }
        public UseModel useModel;
        public void Ini()
        {
            base.SetComponent();
        }
        /// <summary>
        /// 窗体状态
        /// </summary>
        /// <param name="WinType">-1：查看，0：修改，1：增加</param>
        public void SetWinType(int WinType)
        {
            base.WinType = WinType;
        }

        protected override void DisplayComponent(string id)
        {
            base.class_SetTextEdit.SetTextEdit(this.textEdit1, base.WinType < 1 ? true : false, Color.Black);
            base.class_SetTextEdit.SetTextEdit(this.textEdit2, false, Color.Black);
            base.class_SetTextEdit.SetTextEdit(this.textEdit3, false, Color.Black);
            base.class_SetTextEdit.SetTextEdit(this.textEdit4, false, Color.Black);
            if (id != null)
            {
                string Sql = string.Format("Select * From sys_useinfo Where id ='{0}'", id);
                DataTable dataTable = base.class_MySqlDataBase.GetDataTable(Sql);
                if (dataTable != null && dataTable.Rows.Count == 1)
                {
                    DataRow dataRow = dataTable.Rows[0];
                    if (dataRow != null)
                    {
                        this.textEdit1.Text = dataRow["id"].ToString();
                        this.textEdit2.Text = dataRow["useName"].ToString();
                        this.textEdit3.Text = dataRow["nickName"].ToString();
                        this.textEdit4.Text = dataRow["mobile"].ToString();
                    }
                }
            }
        }

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            if (this.textEdit1.Text == "" || this.textEdit1.Text.Length == 0)
            {
                MessageBox.Show("工号不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (this.textEdit2.Text == "" || this.textEdit2.Text.Length == 0)
            {
                MessageBox.Show("用户名不能为空！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (WinType > -1 && base.class_MySqlDataBase.GetKeyCountWithOut("sys_useinfo", "id"
                , "useName", this.textEdit2.Text
                , "id", this.textEdit1.Text) > 0)
            {
                MessageBox.Show("用户名不允许重复！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            if (WinType == 1 && base.class_MySqlDataBase.GetKeyCount("sys_useinfo", "id", "id", this.textEdit1.Text) > 0)
            {
                MessageBox.Show("工号不允许重复！", "温馨提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            useModel = new UseModel();
            useModel.id = this.textEdit1.Text;
            useModel.useName = this.textEdit2.Text;
            useModel.nickName = this.textEdit3.Text;
            useModel.mobile = this.textEdit4.Text;

            this.DialogResult = DialogResult.OK;
        }
    }
}
